Ext.define('Ssimgres.controller.ComentariosArticulo', {
	extend : 'Ext.app.Controller',
	views : ['comentariosArticulo.ComentariosArticuloView', 'comentariosArticulo.ComentarioArticuloView'],
	stores : ['ComentariosArticulo'],
	models : ['ComentarioArticulo'],
	refs : [{
		ref : 'comentariosArticuloView',
		selector : 'comentariosArticuloView'
	}, {
		ref : 'comentarioArticuloView',
		selector : 'comentarioArticuloView'
	}],
	init : function() {
		this.control({
			'comentariosArticuloView button[action=btnVer]' : {
				click : this.comentarioArticuloViewShowVer
			},
			'comentarioArticuloView button[action=btnCancelar]' : {
				click : this.comentarioArticuloViewCancelar
			},
			'comentarioArticuloView button[action=btnGuardar]' : {
				click : this.comentarioArticuloViewGuardar
			},
			'comentariosArticuloView' : {
				afterrender : function() {
					Ext.getCmp('comentariosArticuloView_combo').on('change', this.reset, this);
				},
				itemclick : this.activarBotones
			}
		});
	},
	activarBotones : function() {
		Ext.getCmp('comentariosArticuloView_btnVer').setDisabled(false);
	},
	reset : function(form, value) {
		Ext.getCmp('comentariosArticuloView_btnVer').setDisabled(true);
		Ext.getCmp('comentariosArticuloView_combo').setValue(value);
		this.getComentariosArticuloStore().load({
			params : {
				leido : value
			}
		});
		this.getComentariosArticuloView().getSelectionModel().clearSelections();
	},
	comentarioArticuloViewCancelar : function() {
		this.getComentarioArticuloView().close();
	},
	comentarioArticuloViewShowVer : function() {
		Ext.widget('comentarioArticuloView').show();
		var form = this.getComentarioArticuloView().down('form').getForm();
		form.loadRecord(this.getComentariosArticuloView().getSelectionModel().getSelection()[0]);
	},
	comentarioArticuloViewGuardar : function() {
		var form = this.getComentarioArticuloView().down('form').getForm();
		form.updateRecord();
		if (form.isValid()) {
			form.getRecord().save({
				success : function(operation) {
					Ext.Msg.show({
						title : 'Éxito',
						msg : operation.proxy.reader.jsonData["msg"],
						buttons : Ext.Msg.OK,
						icon : Ext.Msg.INFO
					});
					this.getComentarioArticuloView().close();
					this.reset(null,Ext.getCmp('comentariosArticuloView_combo').getValue());
				},
				failure : function(operation) {
					Ext.Msg.show({
						title : 'Error',
						msg : operation.proxy.reader.jsonData["msg"],
						buttons : Ext.Msg.OK,
						icon : Ext.Msg.ERROR
					});
				},
				scope : this
			});
		} else {
			Ext.Msg.show({
				title : 'Atención',
				msg : 'Verifica los campos del formulario',
				buttons : Ext.Msg.OK,
				icon : Ext.Msg.WARNING
			});
		}

	}
});
